//
//  BookMarksViewController.m
//  HotTopic
//
//  Created by dllo on 16/9/7.
//  Copyright © 2016年 LST. All rights reserved.
//

#import "StylistSonViewController.h"
#import "PWMacror.h"
#import "UIView_extra.h"
#import "BookTableViewCell.h"
#import "CaseViewController.h"
#import "RaidersViewController.h"
#import "InspirationViewController.h"
#import "SharingViewController.h"
#import "DCNavTabBarController.h"
#import "UIImageView+WebCache.h"
#import "AFNetworking.h"
#import "FXBlurView.h"
#import "StyListSonModel.h"
#import "Projects.h"
#import "User.h"
#import "UserImage.h"
#import "UIImageView+WebCache.h"

static CGFloat const headViewHeight = 280;
@interface StylistSonViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) BookTableViewCell * mainTableView;
@property (nonatomic, strong) UIImageView * headImageView;//头部图片
@property (nonatomic, strong) UIImageView * avatarImage;
@property (nonatomic, strong) UILabel * countentLabel;
@property (nonatomic, strong) UIImageView *img;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, assign) BOOL canScroll;
@property (nonatomic, assign) BOOL isTopIsCanNotMoveTabView;
@property (nonatomic, assign) BOOL isTopIsCanNotMoveTabViewPre;
@property (nonatomic, strong) FXBlurView *blurView;
@property (nonatomic, strong)  UIView *barView;
@property (nonatomic, strong) UILabel *titleText;
@property (nonatomic, strong) UIButton *backButton;
@property (nonatomic, strong) UIButton *shareBtn;
@property (nonatomic, strong) Projects *projects;

@end

@implementation StylistSonViewController
@synthesize mainTableView;
/**
 *   收藏夹
 */
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
    [self.navigationController setNavigationBarHidden:YES];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self.view addSubview:self.mainTableView];
    self.mainTableView.tableHeaderView = self.headImageView;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(leaveTop:) name:@"leaveTop" object:nil];
    [self creatView];
//    [self handleData];
}
- (void)creatView {
    _barView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 64)];
    _backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _backButton.frame = CGRectMake(10, 30, 30, 30);
    [_backButton setImage:[UIImage imageNamed:@"返回, 实心"] forState:UIControlStateNormal];
    [_backButton addTarget:self action:@selector(clickBackBtn:) forControlEvents:UIControlEventTouchUpInside];
    _shareBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _shareBtn.frame = CGRectMake(SCREEN_WIDTH - 50, 30, 30, 30);
    [_shareBtn setImage:[UIImage imageNamed:@"分享"] forState:UIControlStateNormal];
    [_shareBtn addTarget:self action:@selector(clickShareBtn:) forControlEvents:UIControlEventTouchUpInside];
    _barView.backgroundColor = [UIColor clearColor];
    [_barView addSubview:_backButton];
    [_barView addSubview:_shareBtn];
    [_barView addSubview:_titleText];
    [self.view addSubview:_barView];
}
- (void)leaveTop:(NSNotification *)notification{
    NSDictionary *userInfo = notification.userInfo;
    NSString *canScroll = userInfo[@"canScroll"];
    if ([canScroll isEqualToString:@"1"]) {
        _canScroll = YES;
    }
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat yOffset  = scrollView.contentOffset.y;
    if (yOffset >= _headImageView.height - 64)  {
        yOffset = _headImageView.height - 64;
    }
    self.navigationController.navigationBar.alpha = (headViewHeight+yOffset)/(headViewHeight-64);
    CGFloat tabOffsetY = [mainTableView rectForSection:0].origin.y - 64.0f;
    CGFloat offsetY = scrollView.contentOffset.y;
    _isTopIsCanNotMoveTabViewPre = _isTopIsCanNotMoveTabView;
    if (offsetY >= tabOffsetY) {
        //不能滑动
        scrollView.contentOffset = CGPointMake(0, tabOffsetY);
        _isTopIsCanNotMoveTabView = YES;
        [_backButton setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
        [_shareBtn setImage:[UIImage imageNamed:@"share"] forState:UIControlStateNormal];
    }else{
        //可以滑动
        _isTopIsCanNotMoveTabView = NO;
        [_backButton setImage:[UIImage imageNamed:@"返回, 实心"] forState:UIControlStateNormal];
        [_shareBtn setImage:[UIImage imageNamed:@"分享"] forState:UIControlStateNormal];
    }
    if (_isTopIsCanNotMoveTabView != _isTopIsCanNotMoveTabViewPre) {
        if (!_isTopIsCanNotMoveTabViewPre && _isTopIsCanNotMoveTabView) {
            [[NSNotificationCenter defaultCenter] postNotificationName:@"goTop" object:nil userInfo:@{@"canScroll":@"1"}];
            _canScroll = NO;
        }
        if(_isTopIsCanNotMoveTabViewPre && !_isTopIsCanNotMoveTabView){
            if (!_canScroll) {
                scrollView.contentOffset = CGPointMake(0, tabOffsetY);
            }
        }
    }
}
- (void)handleData {
    NSString *nameStr = [_urlTitle stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSString *footStr = [@"木木" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSString *strURL = [NSString stringWithFormat:@"http://api.guju.com.cn/v2/user/%@/projects?start=0&count=10&user=%@", nameStr, footStr];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:strURL parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dic = responseObject;
        self.projects = [Projects modelWithDic:dic];
        [mainTableView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"error = %@", error);
    }];
}
- (UIImageView *)headImageView
{
    if (_headImageView == nil)
    {
        _headImageView= [[UIImageView alloc]initWithFrame:CGRectMake(0, 0,SCREEN_WIDTH,headViewHeight)];
        _headImageView.userInteractionEnabled = YES;
        [_headImageView sd_setImageWithURL:[NSURL URLWithString:self.urlHeadImg] placeholderImage:[UIImage imageNamed:@"page2"]completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        }];
        UIBlurEffect *blur = [UIBlurEffect effectWithStyle:UIBlurEffectStyleExtraLight];
        UIVisualEffectView *effectview = [[UIVisualEffectView alloc] initWithEffect:blur];
        effectview.backgroundColor = [UIColor colorWithWhite:0.508 alpha:1.000];
        effectview.alpha = 0.7;
        effectview.frame = CGRectMake(0, 0, SCREEN_WIDTH, headViewHeight);
        [_headImageView addSubview:effectview];
        self.navigationController.hidesBarsOnSwipe = NO;
        _avatarImage = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH / 2 - 25, 20 + 64, 50, 50)];
        _avatarImage.layer.cornerRadius = 25;
        _avatarImage.layer.masksToBounds = YES;
        [_avatarImage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", _urlHeadImg]] placeholderImage:nil];
        [_headImageView addSubview:_avatarImage];
        _avatarImage.userInteractionEnabled = YES;
        [ _avatarImage sd_setImageWithURL:[NSURL URLWithString:self.urlHeadImg] placeholderImage:[UIImage imageNamed:@"page1"]];
        _countentLabel = [[UILabel alloc] initWithFrame:CGRectMake(_avatarImage.frame.origin.x, 20 + 64 + 60, 60, 20)];
        _countentLabel.font = [UIFont systemFontOfSize:15];
        _countentLabel.textColor = [UIColor whiteColor];
        _countentLabel.lineBreakMode = NSLineBreakByWordWrapping;
        _countentLabel.numberOfLines = 0;
        _countentLabel.text = self.urlTitle;
        [_headImageView addSubview:_countentLabel];
        UIButton *takeBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        takeBtn.frame = CGRectMake(SCREEN_WIDTH / 3.5 , headViewHeight - 100, 100,30);
        [takeBtn setTitle:@"＋加关注" forState:UIControlStateNormal];
        [takeBtn setTitleColor:[UIColor whiteColor]  forState:UIControlStateNormal];
        takeBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        [_headImageView addSubview:takeBtn];
        UIButton *contributeBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        contributeBtn.frame = CGRectMake(SCREEN_WIDTH / 2, headViewHeight - 100, 100, 30);
        [contributeBtn setTitle:@"➹私信我" forState:UIControlStateNormal];
        [contributeBtn setTitleColor:[UIColor whiteColor]  forState:UIControlStateNormal];
        contributeBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        contributeBtn.layer.borderColor = [UIColor whiteColor].CGColor;
        contributeBtn.layer.cornerRadius = 5;
        [_headImageView addSubview:contributeBtn];
    }
    return _headImageView;
}
-(UITableView *)mainTableView
{
    if (mainTableView == nil)
    {
        mainTableView= [[BookTableViewCell alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH,SCREEN_HEIGHT)];
        mainTableView.delegate = self;
        mainTableView.dataSource=self;
        mainTableView.bounces = NO;
        mainTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        mainTableView.showsVerticalScrollIndicator = NO;
        mainTableView.backgroundColor = [UIColor clearColor];
        mainTableView.rowHeight = SCREEN_HEIGHT;
    }
    return mainTableView;
}

#pragma marl -tableDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return SCREEN_HEIGHT;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    //添加pageView
    [cell.contentView addSubview:self.setPageViewControllers];
    return cell;
}
-(UIView *)setPageViewControllers{
    CaseViewController *essay = [[CaseViewController alloc] init];
    essay.title = @"案例";
    essay.urlID = _urlTitle;
    RaidersViewController *subscribe = [[RaidersViewController alloc] init];
    subscribe.title = @"攻略";
    InspirationViewController *inspriation = [[InspirationViewController alloc] init];
    inspriation.title = @"灵感集";
    inspriation.urlID = _urlTitle;
    SharingViewController *share = [[SharingViewController alloc] init];
    share.urlStr = _urlTitle;
    share.title = @"分享";
    NSArray *subViewControllers=@[essay, subscribe, inspriation, share];
    DCNavTabBarController *tabBarVC = [[DCNavTabBarController alloc]initWithSubViewControllers:subViewControllers];
    tabBarVC.view.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    [self.view addSubview:tabBarVC.view];
    [self addChildViewController:tabBarVC];
    return tabBarVC.view;
}
- (void)clickBackBtn:(UIBarButtonItem *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)clickShareBtn:(UIBarButtonItem *)sender {
}

@end